﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports DotNetNuke
Namespace NVPortal.Modules.NVNews.Search

    Public MustInherit Class inc_SerachResult
        Inherits Entities.Modules.PortalModuleBase
        Private Datefrom As Date = "01/01/2000"
        Private Dateto As Date = "01/01/2020"
        Private Title As String = ""
        Private KeySearch As String = ""
        Private CategoryId As Integer = 0
        Public CategoryName As String = "Tin tức"
        Private PageIndex As Integer = 1
        Private ControlSearch As Integer = 1
        Private Type As Integer = 1

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not IsPostBack Then
                    'Get info from Url

                    If IsNumeric(Request.Item("sch")) Then
                        ControlSearch = Request.Item("sch")
                        Me.rbtChoice.SelectedValue = ControlSearch
                    End If
                    If IsDate(Request.Item("from")) Then
                        Datefrom = Request.Item("from")
                        If Not Datefrom = "01/01/2000" Then
                            Me.txtDateFrom.Text = Datefrom
                        End If
                    End If
                    If IsDate(Request.Item("to")) Then
                        Dateto = Request.Item("to")
                        If Not Dateto = "01/01/2020" Then
                            Me.txtDateTo.Text = Dateto
                        End If
                    End If
                    If IsNumeric(Request.Item("catid")) Then
                        CategoryId = Request.Item("catid")

                    End If
                    KeySearch = Request.Item("key")
                    Me.txtSearch.Text = Request.Item("key")
                    Title = Request.Item("title")
                    If IsNumeric(Request.Item("pn")) Then
                        PageIndex = Request.Item("pn")
                    End If
                    If IsNumeric(Request.Item("type")) Then
                        Me.Type = Request.Item("type")
                        If Type = 2 Then
                            Me.plSearchAdv.Visible = True
                        Else
                            Me.plSearchAdv.Visible = False
                        End If

                    End If

                    'Bind category
                    BindCate()
                    Me.ddlCategory.SelectedValue = CategoryId
                    'LoadData Search
                    LoadData()
                End If

                Me.imgCalendarPostFrom.Attributes.Add("OnClick", "popUpCalendar(" & Me.txtDateFrom.ClientID & "," & Me.txtDateFrom.ClientID & ", 'mm/dd/yyyy'); return false;")
                Me.imgCalendarPostTo.Attributes.Add("OnClick", "popUpCalendar(" & Me.txtDateTo.ClientID & "," & Me.txtDateTo.ClientID & ", 'mm/dd/yyyy'); return false;")

            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try

        End Sub
#End Region

        Public Sub LoadData()
            'Response.Write("Ok Man!")
            'Dim ctlNews As New NV_NewsController
            'Dim arr As ArrayList
            'arr = ctlNews.NV_News_FindContent(2, PortalId, Datefrom, Dateto, KeySearch, CategoryId)

            'Me.drgNews.DataSource = arr
            'Me.drgNews.PageSize = 10
            'Me.drgNews.CurrentPageIndex = PageIndex - 1
            'Me.drgNews.DataBind()

            'Me.ctlPaging.RowCount = arr.Count
            'Me.ctlPaging.PageIndex = PageIndex
            'Me.ctlPaging.PageSize = 10
            'Me.ctlPaging.BindDataPaging()
            Dim ctlNews As New NV_NewsController
            Dim arr As ArrayList
            Try
                If Me.plSearchAdv.Visible = True Then
                    'Advance Search
                    If IsDate(txtDateFrom.Text) Then
                        Datefrom = Me.txtDateFrom.Text
                    End If
                    If IsDate(txtDateTo.Text) Then
                        Dateto = Me.txtDateTo.Text
                    End If
                    arr = ctlNews.NV_News_FindContent(Me.rbtChoice.SelectedValue, PortalId, Datefrom, Dateto, Me.txtSearch.Text, Me.ddlCategory.SelectedValue)
                    Me.drgNews.DataSource = arr
                    Me.drgNews.PageSize = Me.ddlPageSize.SelectedValue
                    Me.drgNews.CurrentPageIndex = PageIndex - 1
                    Me.drgNews.DataBind()

                    Me.ctlPaging.RowCount = arr.Count
                    Me.ctlPaging.PageIndex = PageIndex
                    Me.ctlPaging.PageSize = Me.ddlPageSize.SelectedValue
                    Me.ctlPaging.BindDataPaging()
                Else
                    'Simple Search
                    'Response.Write("Simple Search")
                    arr = ctlNews.NV_News_FindContent(Me.rbtChoice.SelectedValue, PortalId, "01/31/2008", "12/31/2011", Me.txtSearch.Text, 0)
                    Me.drgNews.DataSource = arr
                    Me.drgNews.PageSize = 10
                    Me.drgNews.CurrentPageIndex = PageIndex - 1
                    Me.drgNews.DataBind()

                    Me.ctlPaging.RowCount = arr.Count
                    Me.ctlPaging.PageIndex = PageIndex
                    Me.ctlPaging.PageSize = 10
                    Me.ctlPaging.BindDataPaging()
                End If

            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try

        End Sub

        Public Sub BindCate()
            Dim ctlCate As New NV_NewsCategoriesController
            Dim arr As ArrayList
            arr = ctlCate.GetAll(PortalId)
            Dim objCate As New NV_NewsCategoriesInfo
            objCate.CategoryID = 0
            objCate.CategoryName = "    ------Tất cả------"
            arr.Insert(0, objCate)
            Me.ddlCategory.DataSource = arr
            Me.ddlCategory.DataTextField = "CategoryName"
            Me.ddlCategory.DataValueField = "CategoryID"
            Me.ddlCategory.DataBind()
        End Sub

        Public Function FormatURL(ByVal sitem As String, ByVal strID As String, ByVal scat As String, ByVal strCatID As String) As String
            Return NavigateURL() & "/" & scat & "/" & strCatID & "/" & sitem & "/" & strID & "/musvn.aspx"
        End Function


        Protected Sub lbtMore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtMore.Click
            If plSearchAdv.Visible = False Then
                plSearchAdv.Visible = True
            Else
                plSearchAdv.Visible = False
            End If
        End Sub

        Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
            If plSearchAdv.Visible = True Then
                Response.Redirect(NavigateURL(TabId) & "/sch/" & rbtChoice.SelectedValue & "/key/" & txtSearch.Text & "/catid/" & ddlCategory.SelectedValue & "/type/2/from/" & Me.txtDateFrom.Text & "/to/" & Me.txtDateTo.Text & "/musvn.aspx")
            Else
                Response.Redirect(NavigateURL(TabId) & "/sch/" & rbtChoice.SelectedValue & "/key/" & txtSearch.Text & "/catid/" & ddlCategory.SelectedValue & "/type/1/from/" & Me.txtDateFrom.Text & "/to/" & Me.txtDateTo.Text & "/musvn.aspx")
            End If
        End Sub
    End Class

End Namespace
